/**
 * @Title:  InitialActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-4-1 上午9:16:50 
 * @version:  V1.0 
 */

package artid.com.arttogoa;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import artid.com.arttogoa.R;

import artid.com.arttogoa.parser.FilesBean;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author 熊彬
 * @data: 2016-4-1 上午9:16:50
 * @version: V1.0
 */
public class InitialActivity extends BaseActivity implements OnClickListener {
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sigmatrix.sdtowifidemo.BaseActivity#onCreate(android.os.Bundle)
	 */
	private ImageView image;
	private TextView info, phone, choice, cancel;
	private RelativeLayout rel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_initial);
		int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
		System.out.println("maxMemory=" + maxMemory);
		mContext = this;
		initData();

	}

	/**
	 * 
	 */
	private void initData() {
		// TODO Auto-generated method stub
		image = (ImageView) findViewById(R.id.iv_add_image);
		info = (TextView) findViewById(R.id.tv_info);
		phone = (TextView) findViewById(R.id.tv_phone);
		choice = (TextView) findViewById(R.id.tv_choice);
		cancel = (TextView) findViewById(R.id.tv_cancel);
		rel = (RelativeLayout) findViewById(R.id.rel_select);
		image.setOnClickListener(this);
		phone.setOnClickListener(this);
		choice.setOnClickListener(this);
		cancel.setOnClickListener(this);
		rel.setOnClickListener(this);
	}

	private AlphaAnimation mHideAnimation = null;
	private AlphaAnimation mShowAnimation = null;

	/**
	 * 
	 * View渐隐动画效果
	 * 
	 * 
	 */

	private void setHideAnimation(View view, int duration) {
		if (null == view || duration < 0) {
			return;
		}
		if (null != mHideAnimation) {
			mHideAnimation.cancel();
		}
		mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
		mHideAnimation.setDuration(duration);
		// mHideAnimation.setFillAfter(true);
		view.startAnimation(mHideAnimation);
	}

	/**
	 * 
	 * View渐现动画效果
	 * 
	 * 
	 */

	private void setShowAnimation(View view, int duration) {
		if (null == view || duration < 0) {
			return;
		}
		if (null != mShowAnimation) {
			mShowAnimation.cancel();
		}
		mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
		mShowAnimation.setDuration(duration);
		mShowAnimation.setFillAfter(true);
		view.startAnimation(mShowAnimation);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.iv_add_image:
			setShowAnimation(rel, 300);
			rel.setVisibility(View.VISIBLE);
			break;
		case R.id.tv_phone:
			setHideAnimation(rel, 300);
			rel.setVisibility(View.GONE);
			Intent intentMe = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(intentMe, 1);
			break;
		case R.id.tv_choice:
			setHideAnimation(rel, 300);
			rel.setVisibility(View.GONE);
			Intent intent = new Intent(mContext, HomeActivity.class);
			startActivityForResult(intent, 2);
			break;
		case R.id.tv_cancel:
			setHideAnimation(rel, 300);
			rel.setVisibility(View.GONE);
			break;
		case R.id.rel_select:
			setHideAnimation(rel, 300);
			rel.setVisibility(View.GONE);
			break;

		default:
			break;
		}
	}

	@SuppressLint("SdCardPath")
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK) {
			String sdStatus = Environment.getExternalStorageState();
			if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
				Log.i("TestFile",
						"SD card is not avaiable/writeable right now.");
				return;
			}
			@SuppressWarnings("static-access")
			String name = new DateFormat().format("yyyyMMdd_hhmmss",
					Calendar.getInstance(Locale.CHINA))
					+ ".jpg";
			Toast.makeText(this, name, Toast.LENGTH_LONG).show();
			Bundle bundle = data.getExtras();
			Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式

			FileOutputStream b = null;
			// ???????????????????????????????为什么不能直接保存在系统相册位置呢？？？？？？？？？？？？
			File file = new File("/sdcard/myImage/");
			file.mkdirs();// 创建文件夹
			String fileName = "/sdcard/myImage/" + name;

			try {
				b = new FileOutputStream(fileName);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				try {
					b.flush();
					b.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			image.setImageBitmap(bitmap);// 将图片显示在ImageView里
		} else if (requestCode == 2 && resultCode == 3) {
			Bundle bundle = data.getExtras();
			FilesBean fb = (FilesBean) bundle.getSerializable("image");
			Bitmap bitmap = getLoacalBitmap(fb.getImgURL()); // 从本地取图片
			image.setImageBitmap(bitmap);
			info.setText("图片名称：" + fb.getName() + "\n图片大小:" + fb.getFileSize());
		} else if (requestCode == 2 && resultCode == 4) {
			Bundle bundle = data.getExtras();
			FilesBean fb = (FilesBean) bundle.getSerializable("image");
			Bitmap bitmap = getLoacalBitmap(fb.getThumbURL()); // 从本地取图片
			image.setImageBitmap(bitmap);
			info.setText("图片名称：" + fb.getName() + "\n图片大小:" + fb.getFileSize());
		}
	}

	/**
	 * 加载本地图片 http://bbs.3gstdy.com
	 * 
	 * @param url
	 * @return
	 */
	public static Bitmap getLoacalBitmap(String url) {
		try {
			FileInputStream fis = new FileInputStream(url);
			return BitmapFactory.decodeStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
}
